/** @file    SecurityFirewall.cpp
 *  @author  Alessandro Polo
 *  @version $Id: SecurityFirewall.cpp 2833 2010-08-02 23:12:57Z alex $
 *  @brief
 * File containing methods for the wosh::SecurityFirewall class.
 * The header for this class can be found in SecurityFirewall.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

 #include <core/SecurityFirewall.h>

 #include <core/ObjectAllocator.h>
 #include <core/WoshKernel.h>
 #include <core/WoshHost.h>
 #include <core/Message.h>
 #include <core/Bus.h>
 #include <core/NetworkChannel.h>
 #include <core/Utilities.h>


using namespace std;
namespace wosh {

 WOSH_REGISTER_INTERFACE(wosh::SecurityPolicy, "wosh::ISerializable", 1.01, _static_SecurityPolicy)
 WOSH_REGISTER(wosh::SecurityFirewall, "wosh::Object", 1.01, _static_SecurityFirewall)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

SecurityPolicy::SecurityPolicy() {
	clear();
 }

void SecurityPolicy::clear() {
	this->policy_id = Utilities::randomLong();
	this->policy_id = _GET_Abs(this->policy_id);
	this->action_succeded = SecurityPolicy::ACTION_UNKNOWN;
	this->action_failed = SecurityPolicy::ACTION_UNKNOWN;
	this->stats_failed = 0;
 }

SecurityPolicy& SecurityPolicy::operator=(const SecurityPolicy& m) {
	if (this == &m) return *this; // Same object?
	this->policy_id = m.policy_id;
	this->action_succeded = m.action_succeded;
	this->action_failed = m.action_failed;
	this->stats_failed = m.stats_failed;
	return *this;
 }

bool SecurityPolicy::operator==(const SecurityPolicy &other) const {
	if ( this->action_succeded != other.action_succeded ) return false;
	if ( this->action_failed != other.action_failed ) return false;
	if ( this->stats_failed != other.stats_failed ) return false;
	return true;
 }

std::string SecurityPolicy::toString() const {
	return Utilities::format("%s#%"PRId64, getClassNameOnlyOf(getClassName()), this->policy_id );
 }

bool SecurityPolicy::updateHash( DataHashContext* hash_context ) const {
	if ( hash_context == NULL ) return false;
	bool ret = true;
	ret = hash_context->update( this->policy_id ) & ret;
	ret = hash_context->update( (int)this->action_succeded ) & ret;
	ret = hash_context->update( (int)this->action_failed ) & ret;
	return ret;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

SecurityFirewall::SecurityFirewall()
	:Object() {
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT SecurityFirewall::validate( const WoshHost* source, const WoshHost* target, const NetworkChannel* channel ) {
	(void)source; (void)target; (void)channel; 
	return WRET_ERR_INTERNAL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT SecurityFirewall::validate( const Message* message, const NetworkChannel* channel ) {
	if ( message == NULL ) return WRET_ERR_PARAM;
	(void)channel; 
	return WRET_ERR_INTERNAL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

WRESULT SecurityFirewall::validate( const Message* message, const Bus* source, const Bus* target ) {
	if ( message == NULL ) return WRET_ERR_PARAM;
	(void)source; (void)target; 
	return WRET_ERR_INTERNAL;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

}; // namespace wosh
